﻿using AppTemplate.UI.Dto;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AppTemplate.UI.Framework
{
    public abstract class PagingViewModel<TPageData> : ViewModel where TPageData : class
    {
        public PagingViewModel()
        {
            this.PageSizes = new List<int> { 20, 30, 50, 100 };
            this.JumpPageCommand = new AsyncRelayCommand<object>(GoToPage);
            this.PageData = new ObservableCollection<TPageData>();
        }
        public AsyncRelayCommand<object> JumpPageCommand { get; private set; }
        private AsyncRelayCommand<int> pageSizeChangedCommand;
        public AsyncRelayCommand<int> PageSizeChangedCommand { get => pageSizeChangedCommand; private set => SetProperty(ref pageSizeChangedCommand, value); }

        public ObservableCollection<TPageData> PageData { get; set; }

        public Task GoToPage(object? parameter)
        {
            if (parameter == null)
            {
                parameter = 1;
            }


            if (parameter is int)
            {
                this.CurrentPage = (int)parameter;
            }
            else
            {
                if (int.TryParse(parameter.ToString(), out var page))
                {
                    this.CurrentPage = page;
                }
            }
            return LoadPageData();
        }

        protected override async Task OnViewLoaded(CancellationToken cancellationToken)
        {
            App.ShowStatusBar(true);
            await LoadPageData();
            this.PageSizeChangedCommand = new AsyncRelayCommand<int>(PageSizeChanged);
        }



        public Task PageSizeChanged(int pageSize)
        {
            this.PageSize = pageSize;
            return this.LoadPageData();
        }
        protected abstract Task LoadPageData();

        private int currentPage = 1;
        public int CurrentPage { get { return currentPage; } set { SetProperty(ref currentPage, value); } }

        private int pageSize;
        public int PageSize
        {
            get
            {
                if (pageSize <= 0)
                {
                    if (this.PageSizes?.Any() != true)
                    {
                        return 20;
                    }
                    else
                    {
                        return this.PageSizes[0];
                    }
                }
                else return pageSize;
            }
            set { SetProperty(ref pageSize, value); }
        }

        private int totalCount = 0;
        public int TotalCount { get { return totalCount; } set { SetProperty(ref totalCount, value); } }

        private int pageCount = 0;
        public int PageCount { get { return pageCount; } set { SetProperty(ref pageCount, value); } }

        public List<int> PageSizes { get; set; }



    }
}
